// pages/person/addressAdd/index.js
const app = getApp()
import Toast from "../../../vant/weapp/toast/toast";
Page({
  data: {
    type: 0, //0是新增，1是修改,2是商品添加地址
    name: "",
    mobile: "",
    address: "",
    is_default: false,
    region: [],
    customItem: "全部",
    multiArray: [],
    multiIndex: [0, 0, 0],
    pid: "",
    id: 0,
    area: 0, //是否显示所选区域，0为不显示，1为显示
    addressId: []
  },

  onLoad(options) {
    const that = this;
    console.log(options);
    const type = options.type;
    app.http.post(app.api.citydata, {
      pid: that.data.pid
    }, (res) => {
      let data = {
        multiArray: that.data.multiArray,
        multiIndex: that.data.multiIndex
      };
      app.http.post(app.api.citydata, {
        pid: res.data[0].id
      }, (res) => {
        data.multiArray[1] = res.data
        app.http.post(app.api.citydata, {
          pid: res.data[0].id
        }, (res) => {
          data.multiArray[2] = res.data
          that.setData(data);
        })
      })

      data.multiArray[0] = res.data
    })
    that.setData({
      type,
    });
    if (type == 1) {
      wx.setNavigationBarTitle({
        title: "修改地址",
      });
      that.getAddressDetail(options.id)
    } else {
      wx.setNavigationBarTitle({
        title: "新增地址",
      });

    }
  },
  getAddressDetail(id) {
    app.http.get(app.api.addressInfo, {
      id
    }, (res) => {
      console.log(res)
      let is_default = res.data.is_default === 1 ? true : false
      let region = [res.data.province, res.data.city, res.data.area]
      this.setData({
        addressId: res.data,
        name: res.data.realname,
        mobile: res.data.mobile,
        address: res.data.address_details,
        region,
        id,
        is_default,
        area: 1
      });
    })
  },
  // 提交
  submit() {
    const that = this
    let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    if (this.data.name == "") {
      Toast.fail("请填写姓名")
      return
    }
    if (!reg.test(this.data.mobile)) {
      Toast.fail("请填写正确的手机号")
      return
    }
    if (this.data.address == "") {
      Toast.fail("请填写详细地址")
      return
    }
    let province_id, city_id, area_id
    if (this.data.type == 1) {
      province_id = this.data.addressId.province_id
      city_id = this.data.addressId.city_id
      area_id = this.data.addressId.area_id
    } else if (this.data.type == 0 || this.data.type == 2) {
      province_id = this.data.multiArray[0][this.data.multiIndex[0]].citycode
      city_id = this.data.multiArray[1][this.data.multiIndex[1]].citycode
      area_id = this.data.multiArray[2][this.data.multiIndex[2]].citycode
    }
    let is_default = this.data.is_default ? "1" : "0"
    app.http.post(app.api.updateAddress, {
      id: this.data.id,
      province_id,
      city_id,
      area_id,
      address_details: this.data.address,
      is_default,
      realname: this.data.name,
      mobile: this.data.mobile,
      remarks: ""
    }, (res) => {
      if (res.code === 200) {
        if (that.data.type == 2) {
          wx.setStorageSync('add', 1)
        }
        setTimeout(() => {
          Toast({
            type: 'success',
            message: res.message,
            forbidClick: true,
            onClose: () => {
              wx.navigateBack({
                delta: 1
              })

            },
          });
        }, 0)

      }
    })
  },
  name(e) {
    console.log(e.detail.value)
    this.setData({
      name: e.detail.value
    })
  },
  mobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  address(e) {
    this.setData({
      address: e.detail.value
    })
  },
  isDefault(e) {
    this.setData({
      is_default: !this.data.is_default,
    });
  },

  bindMultiPickerChange: function (e) {
    let region = [this.data.multiArray[0][e.detail.value[0]].name, this.data.multiArray[1][e.detail.value[1]].name, this.data.multiArray[2][e.detail.value[2]].name]
    this.setData({
      multiIndex: [e.detail.value[0], e.detail.value[1], e.detail.value[2]],
      region,
      area: 1
    });
  },
  // 多选，根据变化列数，来请求数据
  bindMultiPickerColumnChange: function (e) {
    const that = this
    let data = {
      multiArray: that.data.multiArray,
      multiIndex: that.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    if (e.detail.column === 0) {
      app.http.post(app.api.citydata, {
        pid: that.data.multiArray[0][e.detail.value].id
      }, (res) => {
        data.multiArray[1] = res.data
        data.multiIndex[1] = 0;
        app.http.post(app.api.citydata, {
          pid: that.data.multiArray[1][0].id
        }, (res) => {
          data.multiArray[2] = res.data
          data.multiIndex[2] = 0;
          that.setData(data);
        })
      })
    } else if (e.detail.column === 1) {
      app.http.post(app.api.citydata, {
        pid: that.data.multiArray[1][e.detail.value].id
      }, (res) => {
        data.multiArray[2] = res.data
        data.multiIndex[2] = 0;
        that.setData(data);
      })


    }
  },

});